package web.servlet;

import itcast.dao.UserDao;
import itcast.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doGet(req,resp);
    }
 @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     req.setCharacterEncoding("utf-8");
     String username = req.getParameter("username");
     String password = req.getParameter("password");
     User loginuser = new User();
     loginuser.setUsername(username);
     loginuser.setPassword(password);
     UserDao dao = new UserDao();
     User user = dao.login(loginuser);
     if(user==null){
         req.getRequestDispatcher("/failServlet").forward(req,resp);
     }else {
         req.setAttribute("user",user);
         req.getRequestDispatcher("/successServlet").forward(req,resp);

     }


 }
}
